//
//  AppDelegate.m
//  SCM
//
//  Created by huangzhen on 2017/2/7.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "HLogin2ViewController.h"
#import <AFNetworking.h>
#import "UserDefault.h"
#import "ZJNewFeatureController.h"
@interface AppDelegate ()
@property (nonatomic, strong)UINavigationController *authorizedViewController;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //设置电池条的颜色
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    [NSThread sleepForTimeInterval:1.0];
    
    [self rootViewToTabBar];
    
    return YES;
}



-(BOOL) IsLogin{
    
    return [UserDefault isLogin];
}

- (void)rootViewToTabBar {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    BOOL isNew = ![[NSUserDefaults standardUserDefaults]boolForKey:[NSString stringWithFormat:@"ISNEWFEATURE%@",App_Version]];
    
    if (isNew == NO) {
        
        if ([self IsLogin]) {
            [self dismissWithScreenShot];
            
            self.window.rootViewController = [[ViewController alloc]init];
        }else {
            
            HLogin2ViewController * loginViewController = [[HLogin2ViewController alloc]init];
            self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:loginViewController];
        }

//        self.window.rootViewController = [[ViewController alloc]init];
        
    }else{
        
        self.window.rootViewController = [[ZJNewFeatureController alloc]init];
        
    }

    
    
//    @weakify(self);
//    [[RACObserve([HLogin sharedInstance], loginStatus)distinctUntilChanged]subscribeNext:^(id x) {
//        @strongify(self);
//        LoginStatus status = [x integerValue];
//        switch (status) {
//            case LoginStatus_Success:
//                [self dismissWithScreenShot];
//                break;
//            case LoginStatus_Logout:
//            case LoginStatus_Failure:
//                self.window.rootViewController = VC;
//                break;
//            default:
//                break;
//        }
//    }];
}



- (void)dismissWithScreenShot
{
    UIGraphicsBeginImageContextWithOptions(self.window.rootViewController.view.frame.size, YES, 0.0);
    [self.window.rootViewController.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:self.window.frame];
    imageView.image = image;
    self.window.rootViewController = [[ViewController alloc]init];
    [self.window addSubview:imageView];
    @weakify(imageView);
    [UIView animateWithDuration:0.4 animations:^{
        @strongify(imageView);
        imageView.top = ScreenHeight;
    } completion:^(BOOL finished) {
        @strongify(imageView);
        self.authorizedViewController = nil;
        [imageView removeFromSuperview];
    }];
}



- (void)observerNetWorking{
    
    //开始监测
    [[AFHTTPSessionManager manager].reachabilityManager startMonitoring];
    //网络发生变化
    [[AFHTTPSessionManager manager].reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable: {
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"网络错误,请检查网络" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];
            }
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi: {
                NSLog(@"wifi情况下，请求数据");
            }
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN: {
                NSLog(@"蜂窝煤网络环境，请求数据");
            }
                break;
            case AFNetworkReachabilityStatusUnknown: {
                NSLog(@"未知");
            }
                break;
            default:
                break;
        }
    }];
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
